<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class sserver extends MY_Model {
	var $main_server = '10.0.2.35:1703';
	var $main_server_public = '112.78.1.35:1703';
	var $slave_server = array('112.78.1.37:1703','112.78.1.36:1703');
	//var $main_server = '10.0.2.35:1703';
	//var $main_server_public = 's35.gamehaynhat.com';
	//var $slave_server = array('s36.gamehaynhat.com','s37.gamehaynhat.com');
	
	var $balancing_servers =array();
	
	function __construct() {
		parent::__construct();
		//$this->balancing_servers[] = array('link'=> base_url(),'weight'=>10);
		$this->balancing_servers[] = array('link'=>'http://s35.gamehaynhat.com:1703/','weight'=>1);
		$this->balancing_servers[] = array('link'=>'http://s36.gamehaynhat.com:1703/','weight'=>1);
		//$this->balancing_servers[] = array('link'=>'http://s37.gamehaynhat.com:1703/','weight'=>1);
		//$this->balancing_servers[] = array('link'=>'http://cdn37.gamehaynhat.com/','weight'=>1);
	}
	
	
	function rsync(){
		foreach($this->slave_server as $s) {
			$link = 'http://'.$s.'/tsn.php';
			file_get_contents($link);
		}
	}
	
	function balancing_file($patch = '') {
		
		if(count($this->balancing_servers) >0) {
			foreach($this->balancing_servers as $server) {
				if(!isset($server['weight']) || !$server['weight'] || $server['weight'] < 0) 
					$server['weight'] = 1;
				for($i = 0; $i<$server['weight']; $i++) {
					$list_server[] = $server['link'];
				}
				
			}
			
			$count = count($list_server) - 1;
			$rand = rand(0, $count);
			return $list_server[$rand].$patch;
		}
	}
}
